"""
多消费者少生产者模型
"""
import time
from multiprocessing import Process, Queue, current_process


def producer(q: Queue):
    """生产者"""
    for i in range(3):
        time.sleep(1)
        q.put(f'{current_process().name}:第{i}个产品')
    q.put(None)


def consumer(q: Queue):
    """消费者"""
    while True:
        msg = q.get()
        print(msg)
        if msg is None:
            break


if __name__ == '__main__':
    q = Queue(3)
    producer_list = []
    for i in range(5):
        p = Process(target=producer, name=str(i + 1), args=(q,))
        p.daemon = True
        p.start()
        producer_list.append(p)

    consumer_list = []
    for i in range(3):
        p = Process(target=consumer, name=str(i + 1), args=(q,))
        p.daemon = True
        p.start()
        consumer_list.append(p)

    for i in range(5):
        producer_list[i].join()
    for i in range(3):
        consumer_list[i].join()
    print('stop')
